﻿using System;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.Keyence
{
	/// <summary>
	/// 基恩士KV上位链路串口通信的对象,适用于Nano系列串口数据,KV1000以及L20V通信模块，地址格式参考api文档<br />
	/// Keyence KV upper link serial communication object, suitable for Nano series serial data, and L20V communication module, please refer to api document for address format
	/// </summary>
	/// <remarks>
	/// <inheritdoc cref="T:HslCommunication.Profinet.Keyence.KeyenceNanoSerialOverTcp" path="remarks" />
	/// </remarks>
	// Token: 0x0200008E RID: 142
	public class KeyenceNanoSerial : SerialDeviceBase
	{
		/// <summary>
		/// 实例化基恩士的串口协议的通讯对象<br />
		/// Instantiate the communication object of Keyence's serial protocol
		/// </summary>
		// Token: 0x06000AD7 RID: 2775 RVA: 0x00040532 File Offset: 0x0003E732
		public KeyenceNanoSerial()
		{
			base.ByteTransform = new RegularByteTransform();
			base.WordLength = 1;
		}

		/// <inheritdoc />
		// Token: 0x06000AD8 RID: 2776 RVA: 0x000406B0 File Offset: 0x0003E8B0
		protected override OperateResult InitializationOnOpen()
		{
			OperateResult<byte[]> operateResult = base.ReadBase(KeyenceNanoSerialOverTcp.ConnectCmd);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				bool flag2 = operateResult.Content.Length > 2;
				if (flag2)
				{
					bool flag3 = operateResult.Content[0] == 67 && operateResult.Content[1] == 67;
					if (flag3)
					{
						return OperateResult.CreateSuccessResult();
					}
				}
				result = new OperateResult("Check Failed: " + SoftBasic.ByteToHexString(operateResult.Content, ' '));
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000AD9 RID: 2777 RVA: 0x00040734 File Offset: 0x0003E934
		protected override OperateResult ExtraOnClose()
		{
			OperateResult<byte[]> operateResult = base.ReadBase(KeyenceNanoSerialOverTcp.DisConnectCmd);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				bool flag2 = operateResult.Content.Length > 2;
				if (flag2)
				{
					bool flag3 = operateResult.Content[0] == 67 && operateResult.Content[1] == 70;
					if (flag3)
					{
						return OperateResult.CreateSuccessResult();
					}
				}
				result = new OperateResult("Check Failed: " + SoftBasic.ByteToHexString(operateResult.Content, ' '));
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000ADA RID: 2778 RVA: 0x000407B8 File Offset: 0x0003E9B8
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = KeyenceNanoSerialOverTcp.BuildReadCommand(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					OperateResult operateResult3 = KeyenceNanoSerialOverTcp.CheckPlcReadResponse(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
					}
					else
					{
						OperateResult<string, int> operateResult4 = KeyenceNanoSerialOverTcp.KvAnalysisAddress(address);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<byte[]>(operateResult4);
						}
						else
						{
							result = KeyenceNanoSerialOverTcp.ExtractActualData(operateResult4.Content1, operateResult2.Content);
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000ADB RID: 2779 RVA: 0x0004086C File Offset: 0x0003EA6C
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<byte[]> operateResult = KeyenceNanoSerialOverTcp.BuildWriteCommand(address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = KeyenceNanoSerialOverTcp.CheckPlcWriteResponse(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000ADC RID: 2780 RVA: 0x000408E0 File Offset: 0x0003EAE0
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = KeyenceNanoSerialOverTcp.BuildReadCommand(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					OperateResult operateResult3 = KeyenceNanoSerialOverTcp.CheckPlcReadResponse(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						OperateResult<string, int> operateResult4 = KeyenceNanoSerialOverTcp.KvAnalysisAddress(address);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<bool[]>(operateResult4);
						}
						else
						{
							result = KeyenceNanoSerialOverTcp.ExtractActualBoolData(operateResult4.Content1, operateResult2.Content);
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000ADD RID: 2781 RVA: 0x00040994 File Offset: 0x0003EB94
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			OperateResult<byte[]> operateResult = KeyenceNanoSerialOverTcp.BuildWriteCommand(address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = KeyenceNanoSerialOverTcp.CheckPlcWriteResponse(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000ADE RID: 2782 RVA: 0x00040A08 File Offset: 0x0003EC08
		public override string ToString()
		{
			return string.Format("KeyenceNanoSerial[{0}:{1}]", base.PortName, base.BaudRate);
		}
	}
}
